<?php

namespace app\album\controller;

//use app\common\service\FileRemote;

use think\Session;

class Album extends Common
{
    function jcropOp(){
        return view();
    }
    public function photoUploadOp()
    {

        $file = request()->file('photo');
        $info = $file->move(config('param.root_path'));
        if ($info) {
            //session('photo', config('param.root_url') . '/' . str_replace('\\', '/', $info->getSaveName()));
            $this->redirect('crop', [], 302, ['photo' => config('param.root_url') . '/' . str_replace('\\', '/', $info->getSaveName())]);
        } else {
            // 上传失败获取错误信息
            $this->error($file->getError());
        }

    }
    public function cropUploadOp()
    {

        $file = request()->file('crop_img');
        $info = $file->move(config('param.root_path'));
        if ($info) {
            $str_rep = str_replace('\\', '/', $info->getSaveName());
            $url = config('param.root_url') . '/' . $str_rep;
            $local_path = config('param.root_path') . '/' . $str_rep;
            //$remote_path = 'images/' . $str_rep;
            model('common/file_remote', 'service')->upload($local_path, $local_path);
            $userOpenId = session('users.user_openid');
            $openIdWhere['wechat_openid'] = $userOpenId;
            $usersModel = new \app\album\model\Users();
            $userId = $usersModel->findId($openIdWhere);
            if (model('album', 'service')->add($url,$userId)) {
                session('crop_finish_url', $url);
                return json(['status' => 1, 'data' => [], 'msg' => 'success']);
            } else {
                return json(['status' => 0, 'data' => [], 'msg' => "数据添加失败"]);
            }
        } else {
            // 上传失败获取错误信息
            return json(['status' => 0, 'data' => '', 'msg' => $file->getError()]);
        }
    }

    public function cropOp()
    {
        $img_url = request()->get('i');
        $id = Session::get('activity_id');
        $AlbumActivityModel = new \app\album\model\AlbumActivity();
        $imgdata = $AlbumActivityModel->where('a_id',$id['id'])->find()->toArray();
        $album_img = config('param.img_path') .$imgdata['a_album_img'];
        model('Album','service')->downloadImage($album_img);
        $album_img = model('Album','service')->getLocalImgPath($imgdata['a_album_img']);
        $this->assign('id',$id['id']);
        $this->assign('album_img_url',$album_img);
        $this->assign('img_url',$img_url);
        return view();
    }

    public function imgshowOp()
    {
            $id = Session::get('activity_id');
            $this->assign('id',$id['id']);
            $this->assign('img_url', session('crop_finish_url'));
        return view();
    }
//    public function indexOp()
//    {
//        if(request()->isGet()){
//            $img_url = request()->get('i');
//            $id = Session::get('activity_id');
//            $AlbumActivityModel = new \app\album\model\AlbumActivity();
//            $imgdata = $AlbumActivityModel->where('a_id',$id['id'])->find()->toArray();
//            $album_img = config('param.img_path') .$imgdata['a_album_img'];
//            model('Album','service')->downloadImage($album_img);
//            $album_img = model('Album','service')->getLocalImgPath($imgdata['a_album_img']);
//            $this->assign('id',$id['id']);
//            $this->assign('album_img_url',$album_img);
//            $this->assign('img_url',$img_url);
//        }else if(request()->isPost()){
//            $file = request()->post();
//            $imges =  model('Album','service')->baseEncode($file);
//            $image_file = "./uploads/".date("YmdHis").mt_rand(10,99)."a.png";
//            if(file_put_contents($image_file, $imges)){
//                $image_file = substr($image_file,1);
//                model('FileRemote','service')->upload($image_file);
//                $userOpenId = session('users.user_openid');
//                $openIdWhere['wechat_openid'] = $userOpenId;
//                $usersModel = new \app\album\model\Users();
//                $userId = $usersModel->findId($openIdWhere);
//                $data = [
//                    'm_id'      => $userId['users_id'],
//                    'activity_id'=> Session::get('activity_id.id'),
//                    'img_url'   => $image_file,
//                    'a_addtime' => time(),
//                ];
//                $albumModel = new \app\album\model\Album();
//                $imgAddId = $albumModel->imgAdd($data);
//                if($imgAddId){
//                    return json(['code'=>1,'i'=>$image_file]);
//                }else{
//                   return json(['code' => 2]);
//                }
//            }
//        }
//        return view();
//    }
    public function imgchoiceOp(){
        if(request()->isPost()){
            $user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
            if(strpos($user_agent,'iphone')){
                $file = request()->file('images');
                $info =   $file->move(ROOT_PATH . 'public' . DS.'uploadimgs');
                $img_url = '.'.DS.'uploadimgs'.DS.$info->getSaveName();
                model('Album','service')->iphoneImg($img_url);
            }elseif (strpos($user_agent,'android')){
                $file = request()->file('images');
                $info =   $file->move(ROOT_PATH . 'public' . DS.'uploadimgs');
                $img_url = '.'.DS.'uploadimgs'.DS.$info->getSaveName();
            }elseif (strpos($user_agent,'ipad')){
                $file = request()->file('images');
                $info =   $file->move(ROOT_PATH . 'public' . DS.'uploadimgs');
                $img_url = '.'.DS.'uploadimgs'.DS.$info->getSaveName();
                model('Album','service')->iphoneImg($img_url);
            }
            if(file_exists($img_url)){
                $img_url = substr($img_url,1);
                $this->redirect('crop',['i'=>$img_url]);
            }else{
                // 上传失败获取错误信息
                echo '上传失败';
            }
        } else{
            $id = request()->get();
            $AlbumActivityModel = new \app\album\model\AlbumActivity();
            $imgdata = $AlbumActivityModel->where('a_id',$id['id'])->find()->toArray();
            $img_url = config('param.img_path').$imgdata['a_img'];
            Session::set('activity_id',$id);
            $this->assign('img_url',$img_url);
        }
        return view();
    }
}